//
//  DLMineTVHeaderView.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/11/26.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLMineTVHeaderView.h"
#import "DLAssetsView.h"
#import "DLLucreView.h"
#import "DLDealRecordVC.h"
#import "DLTiaoCangRecordVC.h"
#import "DLChiCangRecordVC.h"
#import "DLSettingVC.h"
#import "DLRedeemVC.h"
#import "DLPurchaseVC.h"

@implementation DLMineTVHeaderView

- (instancetype) initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        self.userInteractionEnabled = YES;
        self.backgroundColor = kDLViewNormalBgColor;
        [self layoutSubControls];
    }
    return self;
}

- (void) layoutSubControls {
    
    UIImageView *headImageView = [UIImageView new];
    headImageView.frame = CGRectMake(0, -kHeightNavigationStatusBar, kScreenWidth, kAutoWidth(250));
    headImageView.image = [UIImage imageNamed:@"normal_navbg"];
    headImageView.userInteractionEnabled = YES;
//    headImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self addSubview:headImageView];
    self.headImageView = headImageView;
    
    UIView *falseNavClearView = [UIView new];
    falseNavClearView.frame = CGRectMake(0, 0, kScreenWidth, kHeightNavigation);
    falseNavClearView.backgroundColor = [UIColor clearColor];
    [headImageView addSubview:falseNavClearView];
    
    UIButton *systemSetBtn = [UIButton buttonWithType:0];
    systemSetBtn.frame = CGRectMake(16, kHeightNavigationStatusBar + (44 - 25) / 2, 25, 25);
    [systemSetBtn setImage:[UIImage imageNamed:@"setting"] forState:UIControlStateNormal];
    systemSetBtn.backgroundColor = [UIColor clearColor];
    [falseNavClearView addSubview:systemSetBtn];
    [systemSetBtn addTarget:self action:@selector(systemSetBtnClick) forControlEvents:UIControlEventTouchUpInside];

    UILabel *titleLabel = [UILabel new];
    titleLabel.frame = CGRectMake(90 / 2, systemSetBtn.top, kScreenWidth - 90, systemSetBtn.height);
    titleLabel.text = @"我的";
    titleLabel.font = kBoldFont(16);
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [falseNavClearView addSubview:titleLabel];

    UIButton *machineBtn = [UIButton buttonWithType:0];
    machineBtn.frame = CGRectMake(kScreenWidth - 39 *0.8 - 20, titleLabel.top + (titleLabel.height - 27 *0.8) / 2, 39 *0.8, 27 *0.8);
    [machineBtn setImage:[UIImage imageNamed:@"info_robot"] forState:UIControlStateNormal];
    [falseNavClearView addSubview:machineBtn];
    [machineBtn addTarget:self action:@selector(machineBtnClick) forControlEvents:UIControlEventTouchUpInside];

    DLAssetsView *assetsView = [[DLAssetsView alloc] initWithFrame:CGRectMake(0, falseNavClearView.bottom + kAutoWidth(10) - kHeightNavigationStatusBar, kScreenWidth, kAutoWidth(80))];
    assetsView.backgroundColor = [UIColor clearColor];
    [self addSubview:assetsView];
    self.assetsView = assetsView;
    assetsView.title = @"总资产(元)";
    assetsView.count = @"0.00";
    
    
    CGFloat lucreViewWidth = kScreenWidth / 3;
    CGFloat lucreViewHeight = kAutoWidth(60);
    NSArray *arr = @[@{@"title" : @"昨日收益(元)", @"count" : @"0.00"}, @{@"title" : @"累计收益(元)", @"count" : @"0.00"}, @{@"title" : @"收益率", @"count" : @"0.00%"}];
    for (NSInteger i = 0; i < arr.count; i++) {
        NSDictionary *dic = arr[i];
        DLLucreView *lucreView = [[DLLucreView alloc] initWithFrame:CGRectMake(lucreViewWidth * i, assetsView.bottom, lucreViewWidth, lucreViewHeight) andTitle:dic[@"title"] andCount:dic[@"count"]];
        lucreView.backgroundColor = [UIColor clearColor];
        lucreView.tag = 12138 + i;
        [self addSubview:lucreView];
    }
    
    UIView *redeemAndBuyView = [UIView new];
    redeemAndBuyView.frame = CGRectMake((kScreenWidth - kAutoWidth(190)) / 2, headImageView.bottom - kAutoWidth(20), kAutoWidth(190), kAutoWidth(40));
    redeemAndBuyView.backgroundColor = [UIColor whiteColor];
    redeemAndBuyView.layer.cornerRadius = kAutoWidth(20);
    redeemAndBuyView.clipsToBounds = YES;
    [self addSubview:redeemAndBuyView];
    
    UIButton *redeemBtn = [UIButton buttonWithType:0];
    redeemBtn.frame = CGRectMake(0, 0, (redeemAndBuyView.width - 1) / 2, redeemAndBuyView.height);
    [redeemBtn setTitle:@"赎回" forState:UIControlStateNormal];
    redeemBtn.titleLabel.font = kSystemFont(14);
    [redeemBtn setTitleColor:kDLBtnNormalBgColor forState:UIControlStateNormal];
    [redeemAndBuyView addSubview:redeemBtn];
    [redeemBtn addTarget:self action:@selector(redeemBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *lineView = [UIView new];
    lineView.frame = CGRectMake(redeemBtn.right, redeemBtn.top + kAutoWidth(12), 1, redeemBtn.height - kAutoWidth(24));
    lineView.backgroundColor = kDLBtnNormalBgColor;
    [redeemAndBuyView addSubview:lineView];
    
    UIButton *buyBtn = [UIButton buttonWithType:0];
    buyBtn.frame = CGRectMake(lineView.right, redeemBtn.top, redeemBtn.width, redeemBtn.height);
    [buyBtn setTitle:@"买入" forState:UIControlStateNormal];
    buyBtn.titleLabel.font = kSystemFont(14);
    [buyBtn setTitleColor:kDLBtnNormalBgColor forState:UIControlStateNormal];
    [redeemAndBuyView addSubview:buyBtn];
    [buyBtn addTarget:self action:@selector(buyBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    CGFloat margin = 5;
    CGFloat btnWidth = (kScreenWidth - margin) / 3;
    CGFloat btnHeight = btnWidth;
    
    NSArray *titlesArr = @[@"交易记录", @"调仓记录", @"持仓情况", @"分享给好友"];
    for (NSInteger i = 0; i < titlesArr.count; i++) {
        UIButton *functionButton = [UIButton buttonWithType:0];
        functionButton.frame = CGRectMake((btnWidth + margin) * (i % 3), redeemAndBuyView.bottom + kAutoWidth(10) + (btnHeight + margin) * (i / 3), btnWidth, btnHeight);
        [functionButton setBackgroundColor:[UIColor whiteColor]];
        [functionButton setTitle:titlesArr[i] forState:UIControlStateNormal];
        functionButton.titleLabel.font = kSystemFont(18);
        [functionButton setTitleColor:kDLNormalBlackColor forState:UIControlStateNormal];
        functionButton.tag = 100 + i;
        [self addSubview:functionButton];
        [functionButton addTarget:self action:@selector(functionButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}
- (void) machineBtnClick {
    DLMachineDetailVC *machineDetailVC = [DLMachineDetailVC new];
    [kuserManager.lastVc.navigationController pushViewController:machineDetailVC animated:YES];
}
- (void) systemSetBtnClick {
    DLSettingVC *settingVC = [DLSettingVC new];
    [kuserManager.lastVc.navigationController pushViewController:settingVC animated:YES];
}
- (void) functionButtonClick:(UIButton *)button {
    switch (button.tag - 100) {
        case 0:
        {
            DLDealRecordVC *dealRecordVC = [DLDealRecordVC new];
            [kuserManager.lastVc.navigationController pushViewController:dealRecordVC animated:YES];
        }
            break;
        case 1:
        {
//            DLTiaoCangRecordVC *tiaoCangRecordVC = [DLTiaoCangRecordVC new];
//            [kuserManager.lastVc.navigationController pushViewController:tiaoCangRecordVC animated:YES];
            DLDealRecordVC *dealRecordVC = [DLDealRecordVC new];
            [kuserManager.lastVc.navigationController pushViewController:dealRecordVC animated:YES];
        }
            break;
        case 2:
        {
            DLChiCangRecordVC *chiCangRecordVC = [DLChiCangRecordVC new];
            [kuserManager.lastVc.navigationController pushViewController:chiCangRecordVC animated:YES];
        }
            break;
        case 3:
        {
            
        }
            break;
            
        default:
            break;
    }
}
- (void) buyBtnClick {
    DLPurchaseVC *pvc = [DLPurchaseVC new];
    [kuserManager.lastVc.navigationController pushViewController:pvc animated:YES];
}
- (void) redeemBtnClick {
    DLRedeemVC *rvc = [DLRedeemVC new];
    [kuserManager.lastVc.navigationController pushViewController:rvc animated:YES];
}

- (void) updateUserInfo {
    self.assetsView.count = kuserManager.userInfoModel.assets;
    
    for (NSInteger i = 0; i < 3; i++) {
        DLLucreView *lucerView = [self viewWithTag:12138 + i];
        [lucerView updateUserinfo];
    }
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
